#!/usr/bin/env python
import math

def sym_cnt(sample):
    cnt = {}
    for s in sample:
        try:
            cnt[s] += 1
        except:
            cnt[s] = 1
    return cnt


def prob_distribution(sym_cnt):
    if type(sym_cnt) != dict:
        print "not able to read symbol table"
        return dist
    dist = {}
    total_cnt = sum(sym_cnt.values())
    for (sym, num) in sym_cnt.items():
        p = 1.0 * num / total_cnt
        dist[sym] = p
    return dist

def entropy(sym_cnt):
    e = 0
    for (sym, p) in prob_distribution(sym_cnt).items():
        e += (- p) * math.log(p)
    return e

def inv_dict(d):
    r  = {}
    for (k,v) in d.items():
       r[v] = k
    return r
